<template>
	<view class="info">
		<view class="title">
			<text>{{Info.title}}</text>
		</view>
		<view class="state">
			<text>编辑：{{Info.author}}</text>
			<text>发布时间：{{Info.posttime}}</text>
		</view>
		<view class="contents">
			<image :src="Info.picurl" mode="widthFix"></image>
			<rich-text :nodes="Info.content"></rich-text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				optionns: null,
				Info: {}
			};
		},
		// 监听页面加载onLoad
		onLoad(e) {
			this.optionns = e
			this.getinfo()
		},
		methods: {
			getinfo() {
				// 发送请求
				uni.request({
					url: 'https://ku.qingnian8.com/dataApi/news/detail.php',
					data: this.optionns,
					success: res => {
						res.data.content = res.data.content.replace(/<img/gi, '<img style="max-width:100%"')
						this.Info = res.data
						this.Info.posttime = this.ToTime(res.data.posttime * 1000)
						this.saveHistory()
						// 动态标题
						uni.setNavigationBarTitle({
							title: res.data.title
						});
					}
				})
			},
			// 存储历史记录
			saveHistory() {
				// 得到本地存储historyArr
				let historyArr = uni.getStorageSync("historyArr") || []
				let item = {
					id: this.Info.id,
					classid: this.Info.classid,
					picurl: this.Info.picurl,
					title: this.Info.title,
					lookitem: this.ToTime(Date.now())
				}

				let index = historyArr.findIndex(i => {
					return i.id == this.Info.id
				})
				if (index >= 0) {
					historyArr.splice(index, 1)
				}

				historyArr.unshift(item)
				historyArr = historyArr.splice(0,10)
				// 存储数据到本地
				uni.setStorageSync("historyArr", historyArr)

			},
			// 格式化时间戳
			ToTime(timestamp) {
				var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				var Y = date.getFullYear() + '-';
				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				var D = date.getDate() + ' ';
				var h = date.getHours() + ':';
				var m = date.getMinutes() + ':';
				var s = date.getSeconds();
				return Y + M + D + h + m + s;
			}
		}
	}
</script>

<style lang="scss">
	.info {
		margin: 60rpx 30rpx 0;

		.title {
			text {
				font-size: 40rpx;
				font-weight: bold;
			}
		}

		.state {
			height: 70rpx;
			background-color: #f0f0f0;
			font-size: 26rpx;
			margin: 50rpx 0 40rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
		}

		.contents {

			// 只对后H5有效
			// /deep/ img {
			// 	max-width: 100%;
			// }

			image {
				width: 100%;
			}

			text {
				display: block;
			}
		}
	}
</style>
